<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    a {
      text-decoration: none;
      color: #721c24;
    }

    h1 {
      text-align: center;
      color: #333;
      margin: 20px 0;

    }

    .form {
      width: 900px;
      margin: 50px auto;
      text-align: center;
    }

    .form input,
    .form select {
      width: 80px;
      height: 27px;
      outline: none;
      border-radius: 5px;
      border: 1px solid #b8daff;
      padding-left: 5px;
      box-sizing: border-box;
      margin-right: 15px;
    }

    .form button {
      width: 60px;
      height: 27px;
      background-color: #004085;
      border: 0;
      color: #fff;
      cursor: pointer;
      border-radius: 5px;
    }

    table {
      margin: 0 auto;
      width: 800px;
      border-collapse: collapse;
      color: #004085;
    }

    th,
    td {
      border: 1px solid #b8daff;
    }

    th {
      padding: 10px;
      background: #cfe5ff;
      font-size: 20px;
      font-weight: 400;
    }

    td {
      padding: 10px;
      color: #666;
      text-align: center;
      font-size: 16px;
    }

    tbody tr:hover {
      background: #e1ecf8;
    }
  </style>
</head>

<body>

  <h1>新增学员</h1>
  <form autocomplete="off" class="form" >
    姓名：<input type="text" class="uname" name="uname">
    年龄：<input type="text" class="age" name="age">
    性别：<select class="gender" name="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资：<input type="text"  class="salary" name="salary">
    就业城市：<select  class="city" name="city">
      <option value="赣州">赣州</option>
      <option value="南昌">南昌</option>
      <option value="深圳">深圳</option>
      <option value="上海">上海</option>
      <option value="北京">北京</option>
    </select>
    <button type="submit">录入</button>
  </form>

  <h1>就业榜</h1>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody></tbody>
  </table>


    <script>
        //获取元素
        const form = document.querySelector('form')
        const uname = document.querySelector('.uname')
        const age = document.querySelector('.age')
        const gender = document.querySelector('.gender')
        const salary = document.querySelector('.salary')
        const city = document.querySelector('.city')
        const tbody = document.querySelector('tbody')
        //获取所有带name的元素
        const items = document.querySelectorAll('[name]')
        

        // 声明一个空数组
        const students = []
        //给表单添加事件
        form.addEventListener('submit',function (e) {
            //阻止表单默认行为
            e.preventDefault()  

          //进行表单验证，如果不通过，则不需要添加数据
          for(let j = 0 ;j<items.length;j++){
            if(items[j].value === ''){
              return alert('内容不能为空')
            }
          }

            //创建对象收集数据
            const obj = {
                stuId: students.length+1,
                uname: uname.value,
                age: age.value,
                gender : gender.value,
                salary : salary.value,
                city : city.value
            }
            //  console.log(obj)

            // 追加给数组
            students.push(obj)
            // console.log(students)
            
            // 清空表单  重置 
            this.reset()
            // 调用渲染函数
            fn()
        })
        

        function fn(){
            // 先清空tbody 以前的行 ，把最新数组里面的数据渲染完毕 
            tbody.innerHTML = ''
            for(let i = 0;i<students.length;i++){
                //渲染数据
                const tr = document.createElement('tr')
                tr.innerHTML = `
                    <td>${i+1}</td>
                    <td>${students[i].uname}</td>
                    <td>${students[i].age}</td>
                    <td>${students[i].gender}</td>
                    <td>${students[i].salary}</td>
                    <td>${students[i].city}</td>
                    <td>
                    <a href="javascript:" data-id = ${i}>删除</a>
                    </td>
                `
                 //追加给tbody
                tbody.appendChild(tr)
            }
        
        }


        // 删除模块
        tbody.addEventListener('click',function(e){
            if(e.target.tagName === 'A'){
                //得到当前元素的自定义属性 data-id
                //console.log(e.target.dataset.id)
                students.splice(e.target.dataset.id,1)
                //console.log(arr)

                // 从新渲染一次
                fn()
            }
        })
    </script>


</body>

</html>